<?php

namespace app\api\controller;

use app\api\model\user\UserScoreApply;
use app\common\controller\Api;
use app\common\model\ScoreLog;

class Score extends Api
{
    protected $noNeedLogin = ['ranking'];
    protected $noNeedRight = ['*'];

    //积分排行榜
    public function ranking()
    {
        $user = \app\api\model\User::order('score desc')
            ->field('id,score,nickname,city,level')
            ->paginate(10, false, ['query' => request()->get()]);
        $this->success('',$user);
    }


    //积分转让页展示信息
    public function applyTransferData()
    {
        $receive_user_id = input('receive_user_id',0);//接收人用户id
        $receive_user_info = \app\api\model\User::where(['id'=>$receive_user_id])
            ->field('id,score,nickname,level')->find();
        if (!$receive_user_info)$this->error('积分受让人不存在');
        $user_info = \app\api\model\User::where(['id'=>$this->auth->id])
            ->field('id,score,nickname,level')->find();

        $data = [
            'receive_user_info'=>$receive_user_info,
            'user_info'=>$user_info
        ];

        $this->success('',$data);
    }

    public function getEncryptPassword($password, $salt = '')
    {
        return md5(md5($password) . $salt);
    }


    //转让积分提交
    public function applyTransfer()
    {
        $password = input('password','');
        $score = input('score',0);
        $remark = input('remark','');
        if (empty($password))$this->error('请输入登录密码');

        $apply_model = new UserScoreApply();
        if (empty($score))$this->error('请输入转让积分');

        $user_info = \app\api\model\User::where(['id'=>$this->auth->id])
            ->field('id,score,nickname,password,salt')->find();
        if ($user_info['score']<$score)$this->error('积分数量不足');

        if ($user_info->password != $this->getEncryptPassword($password, $user_info->salt)) {
            $this->error('登录密码错误');
        }

        $data = [
            'receive_user_id'=>input('receive_user_id',0),
            'user_id'=>$this->auth->id,
            'score'=>$score,
            'remark'=>$remark
        ];

        $res = $apply_model->save($data);

        if ($res){
            \app\common\model\User::score(-$data['score'],$data['user_id'],'积分转让',$apply_model->apply_id);
            $this->success('提交成功');
        } else {
            $this->error('提交失败');
        }
    }


    //积分明细
    public function scorelog()
    {
        $list = ScoreLog::where(['user_id'=>$this->auth->id])
            ->order('createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);
        foreach ($list as $k=>$v){
            if ($v['apply_id']){
                $apply_info = UserScoreApply::get($v['apply_id']);

                $list[$k]['apply_info'] = $apply_info;
            }
        }
        $this->success('',$list);
    }

    //转让详情
    public function applyInfo()
    {
        $apply_id = input('apply_id',0);

        $apply_info = UserScoreApply::get($apply_id);

        $receive_user_info = \app\api\model\User::where(['id'=>$apply_info['receive_user_id']])
            ->field('id,score,nickname,gender,birthday,headshot,hobby,personal_intro,mobile,level')
            ->find();
        if (!$receive_user_info)$this->error('积分受让人不存在');
        $user_info = \app\api\model\User::where(['id'=>$apply_info['user_id']])
            ->field('id,score,nickname,level')->find();

        $data = [
            'receive_user_info'=>$receive_user_info,
            'user_info'=>$user_info
        ];

        $this->success('',$data);
    }

    //同意
    public function agree()
    {
        $apply_id = input('apply_id',0);

        $apply_info = UserScoreApply::get($apply_id);
        if ($apply_info->status!=0)$this->error('已审核 ');
        $apply_info->status = 1;
        $apply_info->save();

        $this->success('');
    }

    //拒绝
    public function refuse()
    {
        $apply_id = input('apply_id',0);

        $apply_info = UserScoreApply::get($apply_id);
        if ($apply_info->status!=0)$this->error('已审核 ');
        $apply_info->status = 2;
        $apply_info->save();
        \app\common\model\User::score($apply_info['score'],$apply_info['user_id'],'积分转让拒绝退回');
        $this->success('');

    }
}